package org.sl.flvplayer.flyweights {
	import org.sl.flvplayer.helper.PreviewGenerator;

	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.net.URLRequest;

	/**
	 * Checks whether p_value is a number or a string and depending on the type uses the previewGenerator to either load an external image or take an image from a videoframe.
	 * 
	 * @package org.sl.flvplayer.flyweights
	 * @author Dan
	 * @version 
	 * @see org.sl.flvplayer.data.VideoItem
	 */ 
	public class FWPreviewImage {

		private var previewGenerator : PreviewGenerator;
		private var _isImage : Boolean = false;
		private var _imageURL : URLRequest;
		private var _videoPos : Number;
		private var _isLoaded : Boolean = false;

		/**
		 * bitmap image created from the current video
		 */
		public var image : Bitmap;

		/**
		 * FWPreviewImage constructor processes the parsed parameters in dependency what type p_value is with the help of the PreviewGenerator class.
		 * 
		 * @param p_value is either a <code>String</code> or a <code>Number</code>		
		 * @param p_videoURL path to the source of the video
		 */
		public function FWPreviewImage(p_videoURL : String, p_value : String) {
			previewGenerator =  new PreviewGenerator();
			//checks whether p_value is a number 
			if(isNaN(Number(p_value))) {
				_isImage = true;
				_imageURL = new URLRequest(p_value);
				previewGenerator.renderImage(this, p_value);
			} else {
				_videoPos = Number(p_value);		
				previewGenerator.renderImage(this, p_videoURL, _videoPos);
			}
		}

		/**
		 * Creates a bitmap image and sets _isLoaded to true.
		 * 
		 * @param p_previewImage refer to the specified <code>BitmapData</code> object
		 */
		public function setPreviewImage(p_previewImage : BitmapData) : void {
			image = new Bitmap(p_previewImage, "auto", true);
			_isLoaded = true;
		}

		/**
		 * @return
		 */
		public function isLoaded() : Boolean {
			return _isLoaded;
		}

		/**
		 * @param p_value
		 */
		private function set isImage(p_value : Boolean) : void {
			_isImage = p_value;
		}

		/**
		 * @return
		 */
		public function get isImage() : Boolean {
			return _isImage;
		}

		/**
		 * @param p_value
		 */
		private function set imageURL(p_value : URLRequest) : void {
			_imageURL = p_value;
		}

		/**
		 * @return
		 */
		public function get imageURL() : URLRequest {
			return _imageURL;
		}

		/**
		 * @param p_value 
		 */
		private function set videoPos(p_value : Number) : void {
			_videoPos = p_value;
		}
		
		/**
		 * @return
		 */
		public function get videoPos() : Number {
			return _videoPos;
		}	
	}
}
